﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data;

namespace Timesheet.Common
{
	public static class SessionManager
	{
		#region Session Variables - Put your code here
		#region Sample
		public static string UserName
		{
			set { AddStringValue("UserName", value); }
			get { return GetStringValue("UserName"); }
		}

		public static DateTime? MostRecentVisitDate
		{
			set { AddDateTimeValue("MostRecentVisitDate", value); }
			get { return GetDateTimeValue("MostRecentVisitDate"); }
		}

        public static Student CurrentStudent
        {
            set { AddSerializableUserDefinedTypeValue("CurrentStudent", value); }
            get { return (Student)GetSerializableUserDefinedTypeValue("CurrentStudent"); }
        }
		#endregion

		#region Others
		public static void Abandon()
		{
			SessionHelper.GetSessionHelper().Abandon();
		}

		public static void Clear()
		{
			SessionHelper.GetSessionHelper().Clear();
		}
		#endregion

        #region WishList
        public static Dictionary<long, List<int>> ListProductCategoryIDs
        {
            set { AddSerializableUserDefinedTypeValue("ListProductCategoryIDs", value); }
            get {
                if (GetSerializableUserDefinedTypeValue("ListProductCategoryIDs") == null)
                    return new Dictionary<long, List<int>>();
                return (Dictionary<long, List<int>>)GetSerializableUserDefinedTypeValue("ListProductCategoryIDs"); 
            }
        }
        #endregion
		#endregion

		#region Auxiliary
		public static int SessionHelperInstanceID
		{
			get { return SessionHelper.GetSessionHelper().SessionHelperInstanceID; }
		}

		#region Add Methods
		private static void AddStringValue(string key, string value)
		{
			SessionHelper.GetSessionHelper().AddStringValueToSession(key, value);
		}

        private static void AddListStringValue(string key, List<string> value)
        {
            SessionHelper.GetSessionHelper().AddListStringValueToSession(key, value);
        }

        private static void AddListStringValue(string key, List<int> value)
        {
            SessionHelper.GetSessionHelper().AddListIntValueToSession(key, value);
        }

		private static void AddByteValue(string key, byte? value)
		{
			SessionHelper.GetSessionHelper().AddByteToSession(key, value);
		}

		private static void AddIntValue(string key, int? value)
		{
			SessionHelper.GetSessionHelper().AddIntValueToSession(key, value);
		}

		private static void AddLongValue(string key, long? value)
		{
			SessionHelper.GetSessionHelper().AddLongToSession(key, value);
		}

		private static void AddFloatValue(string key, float? value)
		{
			SessionHelper.GetSessionHelper().AddFloatValueToSession(key, value);
		}

		private static void AddDoubleValue(string key, double? value)
		{
			SessionHelper.GetSessionHelper().AddDoubleValueToSession(key, value);
		}

		private static void AddDecimalValue(string key, decimal? value)
		{
			SessionHelper.GetSessionHelper().AddDecimalValueToSession(key, value);
		}

		private static void AddBoolValue(string key, bool? value)
		{
			SessionHelper.GetSessionHelper().AddBoolValueToSession(key, value);
		}

		private static void AddDateTimeValue(string key, DateTime? value)
		{
			SessionHelper.GetSessionHelper().AddDateTimeValueToSession(key, value);
		}

		private static void AddTimeSpanValue(string key, TimeSpan? value)
		{
			SessionHelper.GetSessionHelper().AddTimeSpanValueToSession(key, value);
		}

		private static void AddSerializableUserDefinedTypeValue(string key, ISerializable value)
		{
			SessionHelper.GetSessionHelper().AddSerializableUserDefinedTypeToSession(key, value);
		}

        private static void AddSerializableUserDefinedTypeValue(string key, IEnumerable<ISerializable> value)
		{
			SessionHelper.GetSessionHelper().AddListSerializableUserDefinedTypeToSession(key, value);
		}
		#endregion

		#region Get Methods
		private static string GetStringValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetStringValueFromSession(key);
		}

        private static List<string> GetListStringValue(string key)
        {
            return SessionHelper.GetSessionHelper().GetListStringValueFromSession(key);
        }

		private static byte? GetByteValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetByteValueFromSession(key);
		}

		private static int? GetIntValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetIntValueFromSession(key);
		}

		private static long? GetLongValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetLongValueFromSession(key);
		}

		private static float? GetFloatValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetFloatValueFromSession(key);
		}

		private static double? GetDoubleValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetDoubleValueFromSession(key);
		}

		private static decimal? GetDecimalValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetDecimalValueFromSession(key);
		}

		private static bool? GetBoolValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetBoolValueFromSession(key);
		}

		private static DateTime? GetDateTimeValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetDateTimeValueFromSession(key);
		}

		private static TimeSpan? GetTimeSpanValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetTimeSpanValueFromSession(key);
		}

		private static object GetSerializableUserDefinedTypeValue(string key)
		{
			return SessionHelper.GetSessionHelper().GetSerializableUserDefinedTypeFromSession(key);
		}
		#endregion
		#endregion

        #region ConsumerSite
        public static List<int> CurrentFriends
		{
			set
			{
                AddListStringValue("CurrentFriends", value);
			}
			get
			{
				return (List<int>)GetSerializableUserDefinedTypeValue("CurrentFriends");
			}
		}

        public static int? DesiresFilterView
        {
            set { AddIntValue("DesiresFilterView", value); }
            get { return GetIntValue("DesiresFilterView"); }
        }

        public static string KeyWord
        {
            set { AddStringValue("DesiresKeyWord", value); }
            get { return GetStringValue("DesiresKeyWord"); }
        }

        public static long? DesiresID
        {
            set { AddLongValue("DesiresID", value); }
            get { return GetLongValue("DesiresID"); }
        }

        public static string SnapURL
        {
            set { AddStringValue("SnapURL", value); }
            get { return GetStringValue("SnapURL"); }
        }

        public static string TwitterAccessToken
        {
            set { AddStringValue("TwitterAccessToken", value); }
            get { return GetStringValue("TwitterAccessToken"); }
        }

        public static string TwitterAccessTokenSecret
        {
            set { AddStringValue("TwitterAccessTokenSecret", value); }
            get { return GetStringValue("TwitterAccessTokenSecret"); }
        }

        public static string UrlReturnAfterLoginTwitterShare
        {
            set { AddStringValue("UrlReturnAfterLoginTwitterShare", value); }
            get { return GetStringValue("UrlReturnAfterLoginTwitterShare"); }
        }

        public static string TwitterFriends
        {
            set { AddStringValue("TwitterFriends", value); }
            get { return GetStringValue("TwitterFriends"); }
        }
        #endregion
	}

	/// <summary>
    /// Student is a user-defined type, so it must be marked as Serializable to best manage Session data in load balancing environment.
	/// When a class is marked as Serializable, the default serialization process will be done automatically 
	/// so you must implement the inteface ISerializable for best performmance
	/// </summary>
	[Serializable()]
    public class Student : ISerializable
    {
        private int _StudentID;
        private string _StudentName;

        public int StudentID { get { return _StudentID; } set { _StudentID = value; } }
        public string StudentName { get { return _StudentName; } set { _StudentName = value; } }

        //Default contrucstor
        public Student()
        {
            StudentID = 0;
            StudentName = string.Empty;
        }

        #region ISerializable Members
        //Deserialization constructor - it's required to be able to instantiate an object by deserializing the stream data get from the State Server
        protected Student(SerializationInfo info, StreamingContext ctxt)
        {
            //Get the values from serialization info and assign them to the appropriate properties
            StudentID = info.GetInt32("StudentID");
            StudentName = info.GetString("StudentName");
        }

        //Serializing the object to stream data to persit into the State Server
        //Just serialize what data you really need to persit instead of the whole object
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            //You can use any custom name for your name-value pair. But make sure you
            //read the values with the same name. For ex:- If you write Student ID as "StudentID"
            //then you should read the same with "StudentID"
            info.AddValue("StudentID", StudentID);
            info.AddValue("StudentName", StudentName);
        }
		#endregion
	}    
}
